package client.superadmin;

import static org.stjs.javascript.JSCollections.$array;
import static org.stjs.javascript.JSCollections.$castArray;

import org.stjs.javascript.Array;
import org.stjs.javascript.Map;

import client.common.EndPoints.SupadminEP;
import client.common.Models.Colegio;
import client.common.Models.EscalaNotas;
import client.common.angular.NgScope;
import client.common.directives.EPChanging;
import client.common.directives.EPLoading;
import client.common.javascript.JsConsole;
import client.common.javascript.JsProcedure0;
import client.common.javascript.JsProcedure1;
import enums.Jornada;

public class ColInfoScope extends NgScope {

	private Colegio colegio;
	private Array<Jornada> jornadas;
	private SupadminEP superEP;
	private EPChanging epChanging;
	private EPLoading epLoading;
	private Long idColegio;
	protected EscalaNotas escalaNotas;

	public ColInfoScope(SupadminEP superEP, Map<String, Long> stateParams,
			EPChanging epChanging, EPLoading epLoading) {
		super("ColInfoScope");
		this.superEP = superEP;
		this.epChanging = epChanging;
		this.epLoading = epLoading;
		this.idColegio = stateParams.$get("idColegio");
		final ColInfoScope that = this;
		colegio = superEP.getColegio(idColegio, new JsProcedure1<Colegio>() {
			public void $invoke(Colegio p) {
				that.escalaNotas = that.superEP.getEscalaNotas(p.id);

			}
		});

		jornadas = $castArray(Jornada.values());
	}

	public static Array<String> $inject() {
		return $array("Superadmin", "$stateParams", "epChanging", "epLoading");
	}

	public void goNext() {
		final ColInfoScope self = this;
		JsConsole.console.log(colegio);
		superEP.goNext(colegio.id, new JsProcedure1<Array<String>>() {

			@Override
			public void $invoke(Array<String> p) {
				JsConsole.console.log("OK, what should we do here?");
				self.colegio = self.superEP.getColegio(self.idColegio);
			}
		});
	}

	public void actualizarColegio() {
		final ColInfoScope that = this;
		epChanging.$invoke(this, 1300, new JsProcedure0() {
			public void $invoke() {
				that.epLoading.$invoke(that.superEP.updateColegio(
						that.colegio.id, that.colegio), "#divInfoColegio");
			}
		});
	}

	public void actualizarEscala() {
		final ColInfoScope that = this;
		epChanging.$invoke(this, 1300, new JsProcedure0() {
			public void $invoke() {
				that.epLoading.$invoke(that.superEP.setEscalaNotas(
						that.colegio.id, that.escalaNotas), "#divInfoColegio");
			}
		});
	}
}
